Completed
Push — Component-MapsGoogle ( 781b97...3bbdb5 )
by
unknown
09:05
created

helper.js ➔ assignMarkerSettings   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 11
rs 9.4285
1
/* globals google */
2
3
/**
4
 * Concatenates default settings and user-defined settings to one object for use in maps object
5
 * @param  {object} location    [object containing latitude, longitude and zoom value]
6
 * @param  {object} mapSettings [google maps api configuration object]
7
 * @param  {object} mapStyles   [google maps styling configuration object]
8
 * @return {object}             [concatenated object with all options]
9
 */
10
export function assignMapsSettings (location, mapSettings, mapStyles) {
11
  Object.assign(mapSettings, {
12
    scrollwheel: false,
13
    styles: mapStyles,
14
    center: {
15
      lat: location.lat,
16
      lng: location.lng
17
    },
18
    zoom: location.zoom
19
  })
20
21
  return mapSettings
22
}
23
24
/**
25
 * Concatenates default settings and user-defined settings to one object for use in marker object
26
 * @param  {object} location        [object containing lat and lang value]
27
 * @param  {object} markerSettings  [google maps api marker configuration object]
28
 * @param  {object} map             [google maps object]
29
 * @return {object}                 [concatenated object with all options]
30
 */
31
export function assignMarkerSettings (location, markerSettings, map) {
32
  Object.assign(markerSettings, {
33
    position: {
34
      lat: location.lat,
35
      lng: location.lng
36
    },
37
    map: map
38
  })
39
40
  return markerSettings
41
}
42
43
export function setMarkerIcon (markerSettings, iconUrl, scaledSizeX = 16, scaledSizeY = 16) {
44
  Object.assign(markerSettings, {
45
    'icon': {
46
      'url': iconUrl,
47
      'scaledSize': new google.maps.Size(scaledSizeX, scaledSizeY),
48
      'origin': new google.maps.Point(0, 0),
49
      'anchor': new google.maps.Point(scaledSizeX / 2, scaledSizeY / 2)
50
    }
51
  })
52
53
  return markerSettings
54
}
55
56
/**
57
 * Adds a Google Maps Info Window to a marker
58
 * @param {string} content  [HTML content for the marker]
59
 * @param {object} marker   [Google maps marker object]
60
 * @param {object} map      [Google maps map object]
61
 * @return {object}         [Google maps info window object]
62
 */
63
export function addInfoWindowToMarker (content, marker, map) {
64
  const infoWindow = new google.maps.InfoWindow({
65
    content: content
66
  })
67
68
  marker.addListener('click', () => {
69
    infoWindow.open(map, marker)
70
  })
71
72
  return infoWindow
73
}
74
75
/**
76
 * Resets a google map to a certain location
77
 * @param {object} location [object containing lat and lang values]
78
 * @param {object} map      [google maps object]
79
 */
80
export function resetMap (location, map) {
81
  map.setCenter({
82
    lat: location.lat,
83
    lng: location.lng
84
  })
85
86
  map.setZoom(location.zoom)
87
}
88
89
/**
90
 * Returns location of a container containing lat, lng and zoom values in data-attributes
91
 * @param  {object} $container [jQuery object of the container]
92
 * @return {object}            [object containing lat, lng and zoom values]
93
 */
94
export function getLocationFromContainer ($container) {
95
  return {
96
    'lat': parseFloat($container.data('lat')),
97
    'lng': parseFloat($container.data('lng')),
98
    'zoom': parseFloat($container.data('zoom'))
99
  }
100
}
101